package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zonglzp.entity.AfterOrderEntity;
import org.jeecg.modules.zonglzp.entity.TeamAfterEntity;
import org.jeecg.modules.zonglzp.entity.teamAfteronepersonEntity;
import org.jeecg.modules.zonglzp.entity.trackAfterEntity;
import org.jeecg.modules.zonglzp.service.CompanyService;
import org.jeecg.modules.zonglzp.service.orderAfterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderAfter")
public class orderAfterController {

    @Autowired
    orderAfterService service;

    @Autowired
    private CompanyService companyservice;

    /*
    查询售后
     */
    @GetMapping("/list")
    public JSONObject orderAfterlist(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String token,
            int orderType,
            String orderSn,   //订单编号条件模糊查询
            String starttime,  //条件查询开始时间
            String endtime   //条件查询结束时间
    ) {

        if ("".equals(starttime))
            starttime = "1949-10-01";
        if ("".equals(endtime))
            endtime = "2099-12-20";

        if (endtime != null) {
            endtime = BusinesswaterController.addDay(endtime, 1);
        }
        String userId = companyservice.getUseridbytoken(token);

        //通过token查询权限代码
        String orgCode=service.getuserorgCode(token);

        JSONObject obj = new JSONObject();
        Page<AfterOrderEntity> page = new Page<AfterOrderEntity>(pageNo, pageSize);
        //这个地方查询集合
        IPage<AfterOrderEntity> pageList = service.getlist(page, userId, orderType, orderSn, starttime, endtime,orgCode);

        if (pageList != null) {
            for (int i = 0; i < pageList.getRecords().size(); i++) {
                pageList.getRecords().get(i).setCreateTime(pageList.getRecords().get(i).getCreateTime().substring(0, 19));
            }
        }
        if (pageList != null) {
            obj.put("data", pageList);
            obj.put("code", 0);
        } else {
            obj.put("data", "暂无信息");
            obj.put("code", 1);
        }
        return obj;
    }


    /*
   查询 每个售后订单的跟踪
     */
    @GetMapping("/getAftertrack")
    public JSONObject getAftertrack(
            @RequestParam(name = "id") String afterOrderId
    ) {
        JSONObject obj = new JSONObject();
        List<trackAfterEntity> aftertrackList = service.getAftertrack(afterOrderId);


        if (aftertrackList != null) {
            //循环进行处理

            //给每个售后跟踪的类型赋值
            for (int i = 0; i < aftertrackList.size(); i++) {
                switch (aftertrackList.get(i).getStatus()) {
                    case 0:
                        aftertrackList.get(i).setStatusString("售后中");
                        break;
                    case 1:
                        aftertrackList.get(i).setStatusString("已拒绝");
                        break;
                    case 2:
                        aftertrackList.get(i).setStatusString("已接受");
                        break;
                    case 3:
                        aftertrackList.get(i).setStatusString("平台收货");
                        break;
                    case 4:
                        aftertrackList.get(i).setStatusString("前端用户准备发货");
                        break;
                    case 5:
                        aftertrackList.get(i).setStatusString("产品已收");
                        break;
                    case 6:
                        aftertrackList.get(i).setStatusString("制作中");
                        break;
                    case 7:
                        aftertrackList.get(i).setStatusString("开始生产");
                        break;
                    case 8:
                        aftertrackList.get(i).setStatusString("后台产品发货");
                        aftertrackList.get(i).setZhuangxiangXs(true);
                        break;
                    case 9:
                        aftertrackList.get(i).setStatusString("售后订单完成已完成");
                        break;
                    case 10:
                        aftertrackList.get(i).setStatusString("前端用户已发货");
                        break;
                    case 11:
                        aftertrackList.get(i).setStatusString("已退款");
                        break;
                    case 12:
                        aftertrackList.get(i).setStatusString("退款被拒绝");
                        break;
                }
                aftertrackList.get(i).setCreateTime(aftertrackList.get(i).getCreateTime().substring(0, 19));

                //判断是不是团订，如果是的话 就去查询这个跟踪记录操作的那波人
                if (aftertrackList.get(i).getType() == 2) {
                    //创建分页的东西
                    Page<TeamAfterEntity> page = new Page<TeamAfterEntity>(1, 10);
                    aftertrackList.get(i).setPageList(service.getteamaftertrackpeoplegroup(page, aftertrackList.get(i).getId()));
                }
            }
            obj.put("data", aftertrackList);
            obj.put("code", 0);
        } else {
            obj.put("data", "暂无数据");
            obj.put("code", 1);
        }
        return obj;
    }


    /*
    根据售后ID获取一个售后详情
     */
    @GetMapping("/getorderatferdetail")
    public JSONObject getorderatferdetail(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();
        AfterOrderEntity entity = service.getorderatferdetail(id);


        //通过个订售后id查询产品名字
        String productName = service.getsingleafterproductName(id);

        //查询面料编号
        String fabric=service.getFabricNumberbyafterId(id);
        if (entity != null) {
            obj.put("data", entity);
            obj.put("msg", productName);
            obj.put("fabricCode", fabric);
            obj.put("code", 0);
        } else {
            obj.put("data", "暂无数据");
            obj.put("code", 1);
        }
        return obj;
    }

    /*
    我的售后中发货
     */
    @GetMapping("/fahuo")
    public JSONObject fahuo(
            @RequestParam(name = "courierId") int courierId,  //快递名称
            @RequestParam(name = "courierNumber") String courierNumber,  //快递单号
            @RequestParam(name = "aftertrackId") String aftertrackId,   //售后订单跟踪表的操作id
            @RequestParam(name = "afterId") int afterId, //售后ID
            @RequestParam(name = "ordertype") int ordertype //订单类型
    ) {

        JSONObject obj = new JSONObject();


        //具体的一些发货操作放service里
        int count = service.fahuo(courierId, courierNumber, aftertrackId, afterId, ordertype);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "发货成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "失败");
        }
        return obj;
    }

    /*
      查询团订售后中该订单需要售后的人员列表
     */
    @GetMapping("/getteamafterpeople")
    public JSONObject getteamafterpeople(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "orderafterId") int orderafterId //售后订单ID
    ) {
        JSONObject obj = new JSONObject();
        Page<TeamAfterEntity> page = new Page<TeamAfterEntity>(pageNo, pageSize);
        IPage<AfterOrderEntity> pageList = service.getteamafterpeople(page, orderafterId);
        if (pageList != null) {
            obj.put("data", pageList);
            obj.put("code", 0);
        } else {
            obj.put("data", "暂无信息");
            obj.put("code", 1);
        }
        return obj;
    }

    /*
    团订售后人员列表中，每个人员的详细参数
     */
    @GetMapping("/getteamAfteronepersonDetail")
    public JSONObject getteamAfteronepersonDetail(
            @RequestParam(name = "afterCode") String afterCode, //售后订单编号
            @RequestParam(name = "customerId") String customerId,   //客户id
            @RequestParam(name = "code") String code, //小产品编号
            int isAfter//售后类型
    ) {
        JSONObject obj = new JSONObject();
        //查询这次的重做返修原因 还有产品的名字
        teamAfteronepersonEntity entity = service.getReasonName(afterCode, customerId,code);
        //获取返修或者重做参数列表 在前端遍历输出
        List<teamAfteronepersonEntity> fanxiuParameter = service.getfanxiuParameter(afterCode, customerId, isAfter,code);
        obj.put("msg", entity);
        obj.put("data", fanxiuParameter);
        return obj;
    }


    /*
      获取个订中 这个人的个订参数
     */
    @GetMapping("/getSingleOrdercanshu")
    public JSONObject getSingleOrdercanshu(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "productId") String productId
    ) {
        JSONObject obj = new JSONObject();

        List<Map<String, Object>> map = service.getSingleOrdercanshu(id,productId);

        obj.put("data", map);
        return obj;
    }


    /*
     查看某个售后小产品的批次信息
     */
    @GetMapping("/getonetrackafterorderpersonpici")
    public JSONObject getonetrackafterorderpersonpici(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();

        Map<String, Object> map = new HashMap<>();
        map = service.getonetrackafterorderpersonpici(id);

        obj.put("data", map);
        obj.put("code", 0);
        return obj;
    }
}
